from flask_sqlalchemy import SQLAlchemy
from .utils.response_util import get_time


db = SQLAlchemy()


class User(db.Model):
    __tablename__ = "user"

    username = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="用户名(学号、工号)")
    password = db.Column(db.String(255), nullable=False, comment="用户密码")
    nickname = db.Column(db.String(255), nullable=True, comment="用户昵称")
    telephone = db.Column(db.Integer, nullable=True, comment="用户联系方式")
    email = db.Column(db.String, nullable=True, comment="用户邮箱")
    user_type = db.Column(db.Integer, nullable=False, comment="用户类型，0-游客 1-学生 2-老师 3-课程负责人 4-系统管理员")
    status = db.Column(db.Boolean, nullable=False, comment="用户状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="用户创建时间")

    def __init__(self, username="", password="", user_type=1):
        self.username = username
        self.password = password
        self.user_type = user_type
        self.status = True
        self.create_time = get_time()


class Course(db.Model):
    __tablename__ = "course"

    course_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="课程ID")
    course_name = db.Column(db.String(255), nullable=False, comment="课程名称")
    chapter = db.Column(db.String(1024), nullable=False, comment="章节名称")
    course_head_id = db.Column(db.Integer, nullable=False, comment="课程负责人ID")
    status = db.Column(db.Boolean, nullable=False, comment="用户状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="用户创建时间")

    def __init__(self, course_name, chapter, course_head_id):
        self.course_name = course_name
        self.chapter = chapter,
        self.course_head_id = course_head_id
        self.status = True
        self.create_time = get_time()


class StudentCourse(db.Model):
    __tablename__ = "student_course"

    student_course_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="学生选择课程的ID")
    student_id = db.Column(db.Integer, nullable=False, comment="选课的学生ID")
    course_id = db.Column(db.Integer, nullable=False, comment="选课的课程ID")
    score = db.Column(db.Float, nullable=True, comment="学生选课的当前成绩")
    status = db.Column(db.Boolean, nullable=False, comment="状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, student_id, course_id):
        self.student_id = student_id
        self.course_id = course_id
        self.score = 0
        self.status = True
        self.create_time = get_time()


class Question(db.Model):
    __tablename__ = "question"

    question_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="问题ID")
    question_type = db.Column(db.Integer, nullable=False, comment="问题类型: 1-填空 2-选择 3-判断 4-程序填空 5-程序改错 6-编程题")
    score = db.Column(db.FLOAT, nullable=False, comment="问题的总分")
    question_add_user_id = db.Column(db.Integer, nullable=False, comment="添加question的用户")
    relate_knowledge = db.Column(db.String(255), nullable=True, comment="问题相关知识点")
    relate_chapter = db.Column(db.String(255), nullable=True, comment="问题相关章节")
    status = db.Column(db.Boolean, nullable=False, comment="问题状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="问题创建时间")

    def __init__(self, question_type, score, relate_chapter, relate_knowledge, question_add_user_id):
        self.question_type = question_type
        self.score = score
        self.relate_chapter = relate_chapter
        self.relate_knowledge = relate_knowledge
        self.question_add_user_id = question_add_user_id
        self.status = True
        self.create_time = get_time()


class QuestionAnswer(db.Model):
    __tablename__ = "question_answer"

    answer_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="答案ID")
    question_id = db.Column(db.Integer, nullable=False, comment="对应问题ID")
    sequence_number = db.Column(db.Integer, nullable=True, default=1, comment="大问题下的子问题所属的位置")
    answer_content = db.Column(db.String(1024), nullable=False, comment="答案内容")
    score = db.Column(db.Integer, nullable=False, comment="分值")
    answer_add_user_id = db.Column(db.Integer, nullable=False, comment="添加答案的用户ID")
    status = db.Column(db.Boolean, nullable=False, comment="问题状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="答案创建时间")

    def __init__(self, question_id, sequence_number, answer_content, score):
        self.question_id = question_id
        self.sequence_number = sequence_number
        self.answer_content = answer_content
        self.score = score
        self.status = True
        self.create_time = get_time()


class UserSubmit(db.Model):
    __tablename__ = "user_submit"

    submit_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="用户提交记录ID")
    user_id = db.Column(db.Integer, nullable=False, comment="用户ID")
    question_id = db.Column(db.Integer, nullable=False, comment="问题ID")
    user_answer = db.Column(db.String(1024), nullable=False, comment="用户提交的答案")
    language = db.Column(db.Integer, nullable=True, comment="用户使用的编程语言: 0-C, 1-C++, 3-Java 6-Python")
    solution_id = db.Column(db.Integer, nullable=True, comment="")
    source_code = db.Column(db.String(1024), nullable=True, comment="源码")
    result = db.Column(db.Integer, nullable=True, comment="结果-分数")
    result_info = db.Column(db.String(255), nullable=False, comment="结果每个点的得分情况")
    code_status = db.Column(db.Integer, nullable=True, comment="代码OJ状态")
    status = db.Column(db.Boolean, nullable=False, comment="提交记录状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="提交记录创建时间")

    def __init__(self, user_id, question_id, language):
        self.user_id = user_id
        self.question_id = question_id
        self.language = language
        self.status = True
        self.create_time = get_time()


class Paper(db.Model):
    __tablename__ = "paper"

    paper_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="试卷ID")
    paper_question = db.Column(db.String(1024), nullable=False, comment="试卷中包含的题目啊")
    paper_add_user_id = db.Column(db.Integer, nullable=False, comment="添加试卷的用户(教师、课程负责人、系统管理员)")
    status = db.Column(db.Boolean, nullable=False, comment="试卷状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="试卷创建时间")

    def __init__(self, paper_question, username):
        self.paper_question = paper_question
        self.paper_add_user_id = username
        self.status = True
        self.create_time = get_time()


class Homework(db.Model):
    __tablename__ = "homework"

    homework_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="作业ID")
    homework_title = db.Column(db.String(255), nullable=False, comment="作业标题")
    homework_visibility = db.Column(db.Boolean, nullable=False, comment="作业可见性")
    start_time = db.Column(db.TIMESTAMP, nullable=False, comment="作业开始时间")
    end_time = db.Column(db.TIMESTAMP, nullable=False, comment="作业结束时间")
    homework_question = db.Column(db.String(1024), nullable=False, comment="作业所包含的题目")
    homework_add_user_id = db.Column(db.Integer, nullable=False, comment="添加作业的管理员ID")
    homework_course = db.Column(db.Integer, nullable=False, comment="作业对应的班级ID")
    status = db.Column(db.Boolean, nullable=False, comment="作业状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="作业创建时间")

    def __init__(self, title, visibility, start, end, questions, user_id, course_id):
        self.homework_title = title
        self.homework_visibility = visibility
        self.start_time = start
        self.end_time = end
        self.homework_question = questions
        self.homework_add_user_id = user_id
        self.homework_course = course_id
        self.status = True
        self.create_time = get_time()


class News(db.Model):
    __tablename__ = "news"

    news_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="新闻ID")
    news_title = db.Column(db.String(255), nullable=False, comment="新闻标题")
    news_content = db.Column(db.String(1024), nullable=False, comment="新闻内容")
    news_add_user_id = db.Column(db.Integer, nullable=False, comment="添加消息的userId")
    status = db.Column(db.Boolean, nullable=False, comment="新闻状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="新闻创建时间")

    def __init__(self, news_title, news_content, news_add_user_id):
        self.news_title = news_title
        self.news_content = news_content
        self.news_add_user_id = news_add_user_id
        self.status = True
        self.create_time = get_time()


class Exam(db.Model):
    __tablename__ = "exam"

    exam_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="考试ID")
    exam_name = db.Column(db.String(255), nullable=False, comment="考试名称")
    paper_id = db.Column(db.Integer, nullable=False, comment="考试所需试卷ID")
    start_time = db.Column(db.TIMESTAMP, nullable=True, comment="考试开始时间")
    end_time = db.Column(db.TIMESTAMP, nullable=True, comment="考试结束时间")
    unlock_secret = db.Column(db.String(10), nullable=True, comment="考试解锁密码")
    exam_add_user_id = db.Column(db.Integer, nullable=False, comment="添加考试的用户ID")
    relate_course = db.Column(db.Integer, nullable=True, comment="对应的班级")
    status = db.Column(db.Boolean, nullable=False, comment="考试状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="考试创建时间")

    def __init__(self, exam_name, paper_id, start_time, end_time, unlock_secret, relate_course, user_id):
        self.exam_name = exam_name
        self.paper_id = paper_id
        self.start_time = start_time
        self.end_time = end_time
        self.unlock_secret = unlock_secret
        self.relate_course = relate_course
        self.exam_add_user_id = user_id
        self.status = True
        self.create_time = get_time()


class StudentHomework(db.Model):
    __tablename__ = "student_homework"

    student_homework_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="学生提交作业ID")
    student_id = db.Column(db.Integer, nullable=False, comment="学生ID")
    homework_id = db.Column(db.Integer, nullable=False, comment="作业ID")
    score = db.Column(db.FLOAT, nullable=True, comment="对应成绩")
    status = db.Column(db.Boolean, nullable=False, comment="作业状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="考试创建时间")

    def __init__(self, user_id, homework_id):
        self.student_id = user_id
        self.homework_id = homework_id
        self.status = True
        self.create_time = get_time()


class StudentExam(db.Model):
    __tablename__ = "student_exam"

    student_exam_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="学生提交考试ID")
    student_id = db.Column(db.Integer, nullable=False, comment="学生ID")
    exam_id = db.Column(db.Integer, nullable=False, comment="考试ID")
    score = db.Column(db.FLOAT, nullable=True, comment="对应成绩")
    status = db.Column(db.Boolean, nullable=False, comment="考试状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="考试创建时间")

    def __init__(self, user_id, exam_id):
        self.student_id = user_id
        self.exam_id = exam_id
        self.status = True
        self.create_time = get_time()


class QuestionOfFillBlank(db.Model):
    __tablename__ = "question_of_fill_blank"

    question_id = db.Column(db.Integer, primary_key=True, comment="填空题ID")
    question_title = db.Column(db.String(255), nullable=False, comment="填空题标题")
    relate_course_id = db.Column(db.Integer, nullable=True, comment="填空题对应课程")
    question_difficulty = db.Column(db.Integer, nullable=True, comment="填空题对应难度")
    question_description = db.Column(db.String(1024), nullable=False, comment="填空题描述: ()代表需填空")
    question_answer = db.Column(db.String(1024), nullable=False, comment="填空题对应答案")
    status = db.Column(db.Boolean, nullable=False, comment="考试状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="考试创建时间")

    def __init__(self, question_id, question_title, relate_course_id, question_difficulty, question_description,
                 question_answer):
        self.question_title = question_title
        self.question_id = question_id
        self.relate_course_id = relate_course_id
        self.question_difficulty = question_difficulty
        self.question_description = question_description
        self.question_answer = question_answer
        self.status = True
        self.create_time = get_time()


class QuestionOfProgram(db.Model):
    __tablename__ = "question_of_program"

    question_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="编程题ID")
    question_title = db.Column(db.String(255), nullable=False, comment="编程题标题")
    relate_course_id = db.Column(db.Integer, nullable=True, comment="编程题对应课程")
    question_difficulty = db.Column(db.Integer, nullable=True, comment="编程题对应难度")
    question_description = db.Column(db.String(1024), nullable=False, comment="编程题描述")
    input_format = db.Column(db.String(255), nullable=False, comment="输入格式")
    output_format = db.Column(db.String(255), nullable=False, comment="输出格式")
    keyword = db.Column(db.String(255), nullable=False, comment="关键字")
    status = db.Column(db.Boolean, nullable=False, comment="编程题状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="编程题创建时间")

    def __init__(self, question_id, question_title, relate_course, question_difficulty, question_description,
                 input_format, output_format, keyword):
        self.question_id = question_id
        self.question_title = question_title
        self.relate_course_id = relate_course
        self.question_difficulty = question_difficulty
        self.question_description = question_description
        self.input_format = input_format
        self.output_format = output_format
        self.keyword = keyword
        self.status = True
        self.create_time = get_time()


class QuestionOfSelect(db.Model):
    __tablename__ = "question_of_select"

    question_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="选择题题ID")
    question_title = db.Column(db.String(255), nullable=False, comment="选择题标题")
    relate_course_id = db.Column(db.Integer, nullable=True, comment="选择题对应课程")
    question_difficulty = db.Column(db.Integer, nullable=True, comment="选择题对应难度")
    question_description = db.Column(db.String(1024), nullable=False, comment="选择题描述")
    wrong_answer = db.Column(db.String(255), nullable=False, comment="选择题错误答案")
    right_answer = db.Column(db.String(255), nullable=False, comment="选择题正确答案")
    status = db.Column(db.Boolean, nullable=False, comment="选择题状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, question_id, question_title, relate_course_id, question_difficulty, question_description,
                 wrong_answer, right_answer):
        self.question_id = question_id
        self.question_title = question_title
        self.relate_course_id = relate_course_id
        self.question_difficulty = question_difficulty
        self.question_description = question_description
        self.wrong_answer = wrong_answer
        self.right_answer = right_answer
        self.status = True
        self.create_time = get_time()


class TeachClass(db.Model):
    __tablename__ = "teach_class"

    teach_class_id = db.Column(db.Integer, autoincrement=True, primary_key=True, comment="教师教授课程ID")
    teacher_id = db.Column(db.Integer, nullable=False, comment="教师ID")
    class_id = db.Column(db.Integer, nullable=False, comment="课程ID")
    status = db.Column(db.Boolean, nullable=False, comment="状态: 0-无效 1-有效")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, teacher_id, class_id):
        self.teacher_id = teacher_id
        self.class_id = class_id
        self.status = True
        self.create_time = get_time()


class QuestionOfProgramFillBlank(db.Model):
    __tablename__ = "question_of_program_fill_blank"

    question_id = db.Column(db.Integer, primary_key=True, comment="程序填空题ID")
    question_title = db.Column(db.String(255), nullable=False, comment="程序填空题标题")
    relate_course_id = db.Column(db.Integer, nullable=False, comment="程序填空题相关课程")
    question_difficulty = db.Column(db.Integer, nullable=False, comment="程序填空题难度")
    question_description = db.Column(db.String(1024), nullable=False, comment="程序填空题描述")
    question_answer = db.Column(db.String(1024), nullable=False, comment="程序填空题答案")
    input_format = db.Column(db.String(255), nullable=False, comment="输入格式")
    output_format = db.Column(db.String(255), nullable=False, comment="输出格式")
    keyword = db.Column(db.String(255), nullable=False, comment="关键字")
    status = db.Column(db.Boolean, nullable=False, comment="状态位")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, question_id, question_title, relate_course_id, question_difficulty, question_description,
                 question_answer, input_format, output_format, keyword):
        self.question_id = question_id
        self.question_title = question_title
        self.relate_course_id = relate_course_id
        self.question_difficulty = question_difficulty
        self.question_description = question_description
        self.question_answer = question_answer
        self.input_format = input_format
        self.output_format = output_format
        self.keyword = keyword
        self.status = True
        self.create_time = get_time()


class QuestionOfJudge(db.Model):
    __tablename__ = "question_of_judge"

    question_id = db.Column(db.Integer, primary_key=True, comment="程序填空题ID")
    question_title = db.Column(db.String(255), nullable=False, comment="程序填空题标题")
    relate_course_id = db.Column(db.Integer, nullable=False, comment="程序填空题相关课程")
    question_difficulty = db.Column(db.Integer, nullable=False, comment="程序填空题难度")
    question_description = db.Column(db.String(1024), nullable=False, comment="程序填空题描述")
    right_answer = db.Column(db.String(1024), nullable=False, comment="程序填空题答案")
    status = db.Column(db.Boolean, nullable=False, comment="状态位")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, question_id, question_title, relate_course_id, question_difficulty, question_description,
                 right_answer):
        self.question_id = question_id
        self.question_title = question_title
        self.relate_course_id = relate_course_id
        self.question_difficulty = question_difficulty
        self.question_description = question_description
        self.right_answer = right_answer
        self.status = True
        self.create_time = get_time()


class QuestionOfProgramModify(db.Model):
    __tablename__ = "question_of_program_modify"

    question_id = db.Column(db.Integer, primary_key=True, comment="程序改错题ID")
    question_title = db.Column(db.String(255), nullable=False, comment="程序改错题标题")
    relate_course_id = db.Column(db.Integer, nullable=False, comment="程序填改错题相关课程")
    question_difficulty = db.Column(db.Integer, nullable=False, comment="程序改错题难度")
    question_description = db.Column(db.String(1024), nullable=False, comment="程序改错题描述")
    right_answer = db.Column(db.String(255), nullable=False, comment="正确答案")
    input_format = db.Column(db.String(255), nullable=False, comment="输入格式")
    output_format = db.Column(db.String(255), nullable=False, comment="输出格式")
    keyword = db.Column(db.String(255), nullable=False, comment="关键字")
    status = db.Column(db.Boolean, nullable=False, comment="状态位")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, question_id, question_title, relate_course_id, question_difficulty, question_description,
                 right_answer, input_format, output_format, keyword):
        self.question_id = question_id
        self.question_title = question_title
        self.relate_course_id = relate_course_id
        self.question_difficulty = question_difficulty
        self.question_description = question_description
        self.right_answer = right_answer
        self.input_format = input_format
        self.output_format = output_format
        self.keyword = keyword
        self.status = True
        self.create_time = get_time()

#
# class PythonException(db.Model):
#     pass


class QuestionTestPoint(db.Model):
    __tablename__ = "question_test_point"

    test_point_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="测试点Id")
    content_type = db.Column(db.Integer, nullable=False, comment="测试类型 0-文件 1-字符串")
    test_point_number = db.Column(db.Integer, nullable=False, comment="测试点在问题中的序号")
    question_id = db.Column(db.Integer, nullable=False, comment="问题Id")
    input_content = db.Column(db.String(255), nullable=True, comment="输入内容")
    output_content = db.Column(db.String(255), nullable=True, comment="输出内容")
    input_file_path = db.Column(db.String(255), nullable=True, comment="输入文件")
    output_file_path = db.Column(db.String(255), nullable=True, comment="输出文件")
    score = db.Column(db.Integer, nullable=False, comment="测试点对应分数")
    status = db.Column(db.Boolean, nullable=False, comment="状态位")
    create_time = db.Column(db.TIMESTAMP, nullable=False, comment="创建时间")

    def __init__(self, content_type, test_point_number, question_id, input_file_path, output_file_path, score):
        self.content_type = content_type
        self.test_point_number = test_point_number
        self.question_id = question_id
        self.input_file_path = input_file_path
        self.output_file_path = output_file_path
        self.score = score
        self.status = True
        self.create_time = get_time()
